package com.qc31.monitorlib.custom

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.qc31.baselib.R
import com.qc31.monitorlib.databinding.ChooseLineViewBinding

/**
 *@Author Jasper
 *@Time   2020/9/28
 *@Desc   自定义选中控件
 */
class ChooseLineView : FrameLayout {
    constructor(context: Context)
            : super(context) {
        initView(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?)
            : super(context, attrs) {
        initView(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
            : super(context, attrs, defStyleAttr) {
        initView(context, attrs)
    }

    private lateinit var binding: ChooseLineViewBinding
    fun initView(context: Context, attrs: AttributeSet?) {
        binding = ChooseLineViewBinding.inflate(LayoutInflater.from(context), this, true)

//        LayoutInflater.from(context).inflate(R.layout.choose_line_view, this)
        val typedArray: TypedArray =
            context.obtainStyledAttributes(attrs, R.styleable.choose_line)
        val startImage =
            typedArray.getResourceId(R.styleable.choose_line_startImg, R.drawable.ic_selected)
        val endImage =
            typedArray.getResourceId(R.styleable.choose_line_endImg, R.drawable.ic_selected)
        val title = typedArray.getString(R.styleable.choose_line_centTitle)
        val startVisibility = typedArray.getInt(R.styleable.choose_line_startVisibility, 1)
        val endVisibility = typedArray.getInt(R.styleable.choose_line_endVisibility, 0)
        val lineVisibility = typedArray.getInt(R.styleable.choose_line_lineVisibility, 1)
        if (startVisibility == 0) {
            binding.ivChooseStart.visibility = View.GONE
        } else {
            binding.ivChooseStart.visibility = View.VISIBLE
        }
        if (endVisibility == 0) {
            binding.ivChooseEnd.visibility = View.GONE
        } else {
            binding.ivChooseEnd.visibility = View.VISIBLE
        }
        if (lineVisibility == 0) {
            binding.vChooseLine.visibility = View.GONE
        } else {
            binding.vChooseLine.visibility = View.VISIBLE
        }

        binding.ivChooseStart.setImageResource(startImage)
        binding.ivChooseEnd.setImageResource(endImage)
        binding.tvChooseTitle.text = title
        typedArray.recycle()
    }

    fun setStartSelect(isSelect: Boolean) {
        binding.ivChooseStart.isSelected = isSelect
    }

    fun setEndSelect(isSelect: Boolean) {
        binding.ivChooseEnd.isSelected = isSelect
    }

    fun setTitle(title: String) {
        binding.tvChooseTitle.text = title
    }

    fun setTitle(titleId: Int) {
        binding.tvChooseTitle.setText(titleId)
    }

    fun setStartVisibility(int: Int) {
        binding.ivChooseStart.visibility = int
    }

    fun setEndVisibility(int: Int) {
        binding.ivChooseEnd.visibility = int
    }
}